package persentation.presentStudent;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import dataservice.ResultMessage;
import vo.chooseCourseVO.ChooseCourseVO;
import vo.courseVO.CourseVO;
import businesslogic.chooseCourseBl.ChooseCourseController;

public class DeleteCoursePanel extends JPanel {

	private ChooseCourseController chooseCourseController;
	private ArrayList<CourseVO> courseVOs;
	private ArrayList<ChooseCourseVO> chooseCourseVOs;
	private JTable table;
	private DefaultTableModel model;
	
	
	public DeleteCoursePanel(ChooseCourseController chooseCourseController) {
		this.chooseCourseController=chooseCourseController;
		setSize(800, 600);
		setLayout(null);
		
		try {
			chooseCourseVOs=chooseCourseController.getAllChooseCourseRecord();
		} catch (Exception e1) {
			
			e1.printStackTrace();
			ExceptionHandler.networkException();
			
		}
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 719, 500);
		this.add(scrollPane);
		
		
		model=new DefaultTableModel(
				new Object[][] {
						{}
					},
					new String[] {
							 "课程编号","课程名","任课老师","上课时间","学分","退选"
					}
				) ;
		table = new JTable(model);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		TableColumn tableColumn=table.getColumnModel().getColumn(5);
		tableColumn.setCellEditor(table.getDefaultEditor(Boolean.class));
		tableColumn.setCellRenderer(table.getDefaultRenderer(Boolean.class));
		table.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if (table.getSelectedColumn()==5&&table.getSelectedRow()!=-1) {
					int i=JOptionPane.showConfirmDialog(null,"确认退选课程？","确认",JOptionPane.YES_NO_OPTION);
					if(i==JOptionPane.YES_OPTION){
						int index=table.getSelectedRow();
						System.out.println(index);
						
						ChooseCourseVO chooseCourseVO=chooseCourseVOs.get(index);
						System.out.println(chooseCourseVO.getCourseName());
						if(deleteCourse(chooseCourseVO)){
							courseVOs.remove(index);
							chooseCourseVOs.remove(index);
							fillTable();
							JOptionPane.showMessageDialog(null,"退选课程成功！","成功！",JOptionPane.INFORMATION_MESSAGE);
						}
						else {
							JOptionPane.showMessageDialog(null,"退选课程失败！","失败！",JOptionPane.INFORMATION_MESSAGE);
						}
					}
					else {
						fillTable();
					}
				}
				
			}
		});

		
		table.setBounds(119, 424, 262, 143);
		scrollPane.setViewportView(table);
		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		fillTable();
		
	}
	
	public void fillTable(){
		try {
			courseVOs=chooseCourseController.getMyDeleteCourse();
			 chooseCourseVOs=chooseCourseController.getDeleteChooseCourseRecord();
		} catch (Exception e) {
			ExceptionHandler.networkException();
		}
		 
		 model = (DefaultTableModel) table.getModel();
		 model.setRowCount(0);
		 for(CourseVO courseVO:courseVOs){
			 Object[] array=new Object[6];
			 array[0]=courseVO.getCourseId();
			 array[1]=courseVO.getCourseName();
			 array[2]=courseVO.getTeacherName();
			 array[3]=courseVO.getCourseTime();
			 array[4]=courseVO.getCredit();
			 array[5]=new Boolean(false);
			
			 
			 model.addRow(array);
			 table.invalidate();
			 table.updateUI();
	      }
	} 
	
	private boolean deleteCourse(ChooseCourseVO chooseCourseVO){
		ResultMessage resultMessage = null;
		try {
			resultMessage = chooseCourseController.deleteAChooseCourse(chooseCourseVO);
		} catch (Exception e) {
			
			e.printStackTrace();
			ExceptionHandler.networkException();
		}
		if (resultMessage==ResultMessage.DELETE_SUCCESS) {
			return true;
		}
		return false;
	}

	
	
	

}
